import CURDHelper from '@/utils/CURDHelper'
import PageConfig from '@/utils/pageConfig'
import { getBtnCtr } from '@/utils/BtnControl'
import Query from '@/components/Query';
import { isNumber } from '@/utils/validate';
import { VxeUI } from 'vxe-pc-ui';
export default {
  name: 'BaseProcess',
  components: {
    Query
  },
  data() {
    const gltxItemRender = {
      name: 'VxeSelect',
      props: {
        multiple: true,
      },
      options: []
    }
    const xxlxItemRender = {
      name: 'VxeSelect',
      props: {
        multiple: false,
      }, 
      options: [],
      props:{
        clearable:true,
      }
    }
    return {
      gltxItemRender,
      xxlxItemRender,
      componentSize: PageConfig.ComponentSize,
      duration: PageConfig.Duration,
      btnPromission: '',
      editModal: false,
      QueryConditions: [],
      BaseApiUrl: '/api/Xmjclcxxzb',
      BaseChildApiUrl: '/api/Xmjclcxxfb',
      editRow: undefined,
      QueryConfig: [{
        title: '流程模板名称',
        field: 'lcmbmc',
        type: 'input',
        value: '',
        placeholder: '请输入流程名称以查询',
      }
      ],
      GridOptions: {
        id:'BaseProcess_MainGrid',
        border: true,
        stripe: true,
        loading: false,
        size: PageConfig.ComponentSize,
        height: 350,
        columnConfig: {
          resizable: true
        },
        rowConfig: {
          isHover: true
        },
        checkboxConfig: {
          highlight: true,
          range: true
        },
        exportConfig: {
        },
        toolbarConfig: {
          custom: true,
          export: true,
          slots: {
            buttons: 'customButtons'
          }
        },
        customConfig:{
          mode:'drawer',
          storage:true
        },
        columns: [
          { type: 'seq', width: 50 },
          { type: 'checkbox', width: 35 },
          { field: 'lcmbmc', title: '流程模板名称', sortable: true, minWidth: 100, showOverflow: true },
          { field: 'gltxid', title: '关联体系ID', sortable: true, minWidth: 100, showOverflow: true, visible: false },
          { field: 'gltxmc', title: '关联体系名称', sortable: true, minWidth: 300, showOverflow: true },
          { field: 'lcmbbz', title: '流程模板备注', sortable: true, minWidth: 300, showOverflow: true },
          { field: 'createtime', title: '创建时间', minWidth: 150, showOverflow: true },
          { field: 'createname', title: '创建人', minWidth: 100, showOverflow: true },
          { field: 'updatetime', title: '更新时间', minWidth: 150, showOverflow: true },
          { field: 'updatename', title: '更新人', minWidth: 100, showOverflow: true },
          { field: 'operation', title: '操作', fixed: 'right', width: 120, slots: { default: 'operation' } },
        ],
        data: []
      },
      PagerConfig: {
        align: 'center',
        pageSize: 25,
        total: 0,
        currentPage: 1,
        pageSizes: [10, 25, 50, 100, 200],
      },
      FormOptions: {
        'title-width': 140,
        border: true,
        titleBackground: true,
        data: {
          id: -1,
          createid: '',
          createtime: '',
          createname: '',
          updateid: '',
          updatetime: '',
          updatename: '',
          lcmbmc: '',
          gltxid: [],
          gltxmc: undefined,
          lcmbbz: '',
        },
        rules: {
          lcmbmc: [{ required: true, message: '请输入流程模板名称', trigger: 'blur' }],
          gltxid: [{ required: true, message: '请选择关联体系', trigger: 'blur' }]
        },
        items: [
          { field: 'lcmbmc', title: '流程模板名称', span: 24, itemRender: { name: 'VxeInput' }, placeholder: '请输入流程模板名称' },
          { field: 'gltxid', title: '关联体系', span: 24, itemRender: gltxItemRender, placeholder: '请选择关联体系' },
          { field: 'lcmbbz', title: '流程模板备注', span: 24, itemRender: { name: 'VxeTextarea', maxlegnth: 500, clearable: true, showWordCount: true }, placeholder: '请输入备注' },
          {
            align: 'center',
            span: 24,
            itemRender: {
              name: 'VxeButtonGroup',
              options: [{
                type: 'submit', content: '提交', status: 'primary'
              }, {
                type: 'reset', content: '重置'
              }]
            }
          }
        ]
      },
      ChildGridOptions: {
        id:'BaseProcess_ChildGrid',
        border: true,
        stripe: true,
        loading: false,
        size: PageConfig.ComponentSize,
        height: 350,
        columnConfig: {
          resizable: true
        },
        rowConfig: {
          isHover: true,
          isCurrent:true
        },
        checkboxConfig: {
          highlight: true,
          range: true
        },
        editConfig: {
          trigger: 'click',
          mode: 'row'
        },
        exportConfig: {
        },
        toolbarConfig: {
          custom: true,
          slots: {
            tools: 'customTools'
          }
        },
        customConfig:{
          mode:'drawer',
          storage:true
        },
        columns: [
          { type: 'seq', width: 50 },
          { field: 'lcmc', title: '流程名称', sortable: true, minWidth: 300, showOverflow: true, editRender: { name: 'VxeInput' } },
          { field: 'lcxh', title: '流程顺序', sortable: true, minWidth: 100, showOverflow: true, editRender: { name: 'VxeInput' } },
          { field: 'sfxcsh', title: '是否现场审核', sortable: true, minWidth: 100, showOverflow: true, editRender: { name: 'VxeSelect' 
            ,options: [{ label: '是', value: 'Y' }, { label: '否', value: 'N' }]}},
          { field: 'lcjdid', title: '消息提醒', minWidth: 150, showOverflow: true, editRender: xxlxItemRender},
          { field: 'lcbz', title: '流程备注', sortable: true, minWidth: 300, showOverflow: true, editRender: { name: 'VxeInput' } },
          { field: 'lczid', title: '流程模板ID', visible: false },
          { field: 'operation', title: '操作', fixed: 'right', width: 120, slots: { default: 'operation' } },
        ],
        data: []
      },
    }
  },
  created() {
    // 动态头部按钮
    this.btnPromission = getBtnCtr(
      this.$route.path,
      this.$store.getters.userPermission
    )
    this.GetTxOptions();
    this.GetXxlxOptions();
  },
  methods: {
    ValidatorNumber(e) {
      if (!isNumber(e.itemValue)) {
        return new Error('请输入数字');
      } else {
        if (e.itemValue < 0) {
          return new Error('请输入大于等于0的数字');
        }
      }
    },
    handleQuery(e) {
      this.QueryConditions = e;
      this.DoQuery(e);
    },
    handlePageChange() {
      this.DoQuery(this.QueryConditions);
    },
    handleBatchDelete() {
      let rows = this.$refs.maingrid.getCheckboxRecords(true);
      this.DoBatchDelete(rows);
    },
    handleAdd() {
      this.editModal = true;
    },
    handleEdit(row) {
      this.FormOptions.data = row;
      this.FormOptions.data.gltxid = row.gltxid.split(',').map(Number);
      this.editModal = true;
    },
    handleDelete(row) {
      this.DoDelete(row);
    },
    handleEditModalSubmit() {
      this.FormOptions.data.id == -1 ? this.DoAdd() : this.DoEdit();
    },
    handleEditModalReset() {

    },
    handleChildCancel() {
      this.$refs.childgrid.clearEdit();
    },
    handleChildAdd() {
      if (!this.editRow) {
        VxeUI.modal.message({ content: '请双击选择流程模板', status: 'error', duration: this.duration })
      }
      this.ChildGridOptions.data.push({
        id: 0,
        lcmc: '',
        lcxh: '',
        lcbz: '',
        sfxcsh: 'Y',
        lczid: this.editRow.id,
        lcjdid:undefined
      })
    },
    handleChildEdit(row) {
      this.$refs.childgrid.setEditRow(row);
    },
    hasEditStatus(row) {
      return this.$refs.childgrid.isEditByRow(row);
    },
    handleChildSave(row) {
      row.id == 0? this.DoChildAdd(row) : this.DoChildEdit(row);
    },
    handleCellDbClick(e) {
      this.editRow = e.row;
      this.$refs.maingrid.setAllCheckboxRow(false);
      this.$refs.maingrid.setCheckboxRow(e.row, true)
      let queryConditions = [{
        column: 'lczid',
        content: e.row.id,
        condition: '0'
      }]
      this.DoChildQuery(queryConditions);
    },
    handleChildDelete(row) {
      this.DoChildDelete(row);
    },
    DoQuery(e) {
      let request = {
        isPaging: true,
        pageSize: this.PagerConfig.pageSize,
        pageIndex: this.PagerConfig.currentPage,
        queryConditions: e,
        orderByConditions: []
      }
      CURDHelper.Query(request, this.BaseApiUrl)
        .then(res => {
          this.GridOptions.data = res.data;
          this.PagerConfig.total = res.totalNumber;
        }).catch(err => {
          VxeUI.modal.message({
            content: `${err}`,
            status: 'error',
            duration: this.duration
          })
        })
    },
    DoAdd() {
      let postData = {
        id: 0,
        lcmbmc: this.FormOptions.data.lcmbmc,
        gltxid: this.FormOptions.data.gltxid.join(','),
        gltxmc: '',
        lcmbbz: this.FormOptions.data.lcmbbz
      }
      this.FormOptions.data.gltxid.forEach(item => {
        this.gltxItemRender.options.forEach(item2 => {
          if (item2.value == item) {
            postData.gltxmc += item2.label + ',';
          }
        })
      })
      postData.gltxmc = postData.gltxmc.substring(0, postData.gltxmc.length - 1);
      //新增
      CURDHelper.Add(postData, null, this.BaseApiUrl)
        .then(res => {
          VxeUI.modal.message({
            content: '新增数据成功',
            status: 'success',
            duration: this.duration
          })
          this.$refs.editForm.reset();
          this.FormOptions.data.id = -1;
          this.editModal = false;
          this.DoQuery(this.QueryConditions);
        }).catch(err => {
          VxeUI.modal.message({ content: `${err}`, status: 'error', duration: this.duration })
        })
    },
    DoEdit() {
      let postData = {
        id: this.FormOptions.data.id,
        lcmbmc: this.FormOptions.data.lcmbmc,
        gltxid: this.FormOptions.data.gltxid.join(','),
        gltxmc: '',
        lcmbbz: this.FormOptions.data.lcmbbz
      }
      this.FormOptions.data.gltxid.forEach(item => {
        this.gltxItemRender.options.forEach(item2 => {
          if (item2.value == item) {
            postData.gltxmc += item2.label + ',';
          }
        })
      })
      postData.gltxmc = postData.gltxmc.substring(0, postData.gltxmc.length - 1);
      //编辑
      CURDHelper.EditWithConfirm('是否修改当前数据', postData, null, this.BaseApiUrl)
        .then(res => {
          VxeUI.modal.message({
            content: '编辑数据成功',
            status: 'success',
            duration: this.duration
          })
          this.$refs.editForm.reset();
          this.FormOptions.data.id = -1;
          this.editModal = false;
          this.DoQuery(this.QueryConditions);
        }).catch(err => {
          VxeUI.modal.message({
            content: `${err}`,
            status: 'error',
            duration: this.duration
          })
        })
    },
    DoDelete(row) {
      //删除
      let deleteModel = {
        ID: row.id,
        DeleteRemark: ''
      };
      CURDHelper.DeleteWithConfirm('是否删除当前数据', deleteModel, null, this.BaseApiUrl)
        .then(res => {
          VxeUI.modal.message({
            content: '删除数据成功',
            status: 'success',
            duration: this.duration
          })
          this.DoQuery(this.QueryConditions);
        }).catch(err => {
          VxeUI.modal.message({
            content: `${err}`,
            status: 'error',
            duration: this.duration
          })
        })
    },
    DoBatchDelete(rows) {
      //删除
      let deleteList = [];
      rows.forEach(row => {
        deleteList.push({
          ID: row.id,
          DeleteRemark: ''
        })
      })
      CURDHelper.DeleteWithConfirm('是否删除当前已选择数据，删除后不能恢复', null, deleteList, this.BaseApiUrl)
        .then(res => {
          VxeUI.modal.message({
            content: '删除数据成功',
            status: 'success',
            duration: this.duration
          })
          this.DoQuery(this.QueryConditions);
        }).catch(err => {
          VxeUI.modal.message({
            content: `${err}`,
            status: 'error',
            duration: this.duration
          })
        })
    },
    GetTxOptions() {
      let request = {
        isPaging: false,
        pageSize: 1,
        pageIndex: 1,
        queryConditions: [],
        orderByConditions: []
      }
      CURDHelper.Query(request, '/api/Xmjcgltx')
        .then(res => {
          this.gltxItemRender.options = [];
          res.data.forEach(item => {
            this.gltxItemRender.options.push({
              value: item.id,
              label: item.txmc
            })
          })
        }).catch(err => {
          VxeUI.modal.message({
            content: `${err}`,
            status: 'error',
            duration: this.duration
          })
        })
    },
    DoChildAdd(row) {
      this.$refs.childgrid.clearEdit();
      let postData = {
        id: row.id,
        lcmc: row.lcmc,
        lcxh: row.lcxh,
        lcbz: row.lcbz,
        lczid:row.lczid,
        sfxcsh: row.sfxcsh,
        lcjdid:row.lcjdid
      }
      //新增
      CURDHelper.Add(postData, null, this.BaseChildApiUrl)
        .then(res => {
          VxeUI.modal.message({
            content: '新增数据成功',
            status: 'success',
            duration: this.duration
          })
          this.DoChildQuery([{
            column: 'lczid',
            content: this.editRow.id,
            condition: '0'
          }]);
        }).catch(err => {
          VxeUI.modal.message({ content: `${err}`, status: 'error', duration: this.duration })
        })
    },
    DoChildEdit(row) {
      this.$refs.childgrid.clearEdit();
      let postData = {
        id: row.id,
        lcmc: row.lcmc,
        lcxh: row.lcxh,
        lcbz: row.lcbz,
        lczid:row.lczid,
        sfxcsh: row.sfxcsh,
        lcjdid:row.lcjdid
      }
      //新增
      CURDHelper.Edit(postData, null, this.BaseChildApiUrl)
        .then(res => {
          VxeUI.modal.message({content: '编辑数据成功',status: 'success',duration: this.duration})
          this.DoChildQuery([{
            column: 'lczid',
            content: this.editRow.id,
            condition: '0'
          }]);
        }).catch(err => {
          VxeUI.modal.message({ content: `${err}`, status: 'error', duration: this.duration })
        })
    },
    DoChildQuery(e) {
      let queryConditions = e || [];
      let request = {
        isPaging: false,
        pageSize: 1,
        pageIndex: 1,
        queryConditions: queryConditions,
        orderByConditions: [{
          column: 'lcxh',
          condition: 'asc'
        }]
      }
      CURDHelper.Query(request, this.BaseChildApiUrl)
        .then(res => {
          this.ChildGridOptions.data = res.data;
        }).catch(err => {
          VxeUI.modal.message({
            content: `${err}`,
            status: 'error',
            duration: this.duration
          })
        })
    },
    DoChildDelete(row) {
      //删除
      let deleteModel = {
        ID: row.id,
        DeleteRemark: ''
      };
      CURDHelper.DeleteWithConfirm('是否删除当前数据', deleteModel, null, this.BaseChildApiUrl)
        .then(res => {
          VxeUI.modal.message({content: '删除数据成功',status: 'success',duration: this.duration})
          this.DoChildQuery();
        }).catch(err => {
          VxeUI.modal.message({content: `${err}`,status: 'error',duration: this.duration})
        })
    },
    GetXxlxOptions() {
      let request = {
        isPaging: false,
        pageSize: 1,
        pageIndex: 1,
        queryConditions: [{
          column:'lcjd',
          condition:'0',
          content:'Y'
        }],
        orderByConditions: []
      }
      CURDHelper.Query(request, '/api/Tpmmessagetype')
        .then(res => {
          this.xxlxItemRender.options = [];
          res.data.forEach(item => {
            this.xxlxItemRender.options.push({
              value: item.id,
              label: item.xxlx
            })
          })
        }).catch(err => {
          VxeUI.modal.message({
            content: `${err}`,
            status: 'error',
            duration: this.duration
          })
        })
    },

  }
}
